const SkipList = require("../template")

function testSkipListBasic() {
  const sl = new SkipList()

  console.log(
    "1. 基本插入测试:",
    (() => {
      sl.insert(1)
      sl.insert(3)
      sl.insert(2)
      return sl.search(1) === true &&
             sl.search(2) === true &&
             sl.search(3) === true &&
             sl.search(4) === false
    })()
  )

  console.log(
    "2. 基本删除测试:",
    (() => {
      sl.delete(2)
      return sl.search(2) === false &&
             sl.search(1) === true &&
             sl.search(3) === true
    })()
  )

  console.log(
    "3. 跳表大小测试:",
    (() => {
      return sl.getSize() === 2
    })()
  )

  console.log(
    "4. 跳表层数测试:",
    (() => {
      return sl.getLevel() >= 0
    })()
  )

  console.log(
    "5. 空跳表测试:",
    (() => {
      const emptySl = new SkipList()
      return emptySl.isEmpty() === true &&
             emptySl.getSize() === 0 &&
             emptySl.getMin() === null &&
             emptySl.getMax() === null
    })()
  )

  console.log(
    "6. 最小值最大值测试:",
    (() => {
      return sl.getMin() === 1 &&
             sl.getMax() === 3
    })()
  )

  console.log(
    "7. 获取所有值测试:",
    (() => {
      const values = sl.getAllValues()
      const expected = [1, 3]
      return JSON.stringify(values.sort()) === JSON.stringify(expected.sort())
    })()
  )

  console.log(
    "8. this上下文跳表测试:",
    (() => {
      const slObj = {
        multiplier: 2,
        processSkipList: function(values) {
          const sl = new SkipList()
          values.forEach(val => sl.insert(val))
          return sl.getSize() * this.multiplier
        }
      }
      return slObj.processSkipList([1, 2, 3]) === 6 // 3*2=6
    })()
  )

  console.log(
    "9. 复杂对象this绑定跳表测试:",
    (() => {
      const slConfigObj = {
        config: { threshold: 2 },
        processWithConfig: function(values) {
          const sl = new SkipList()
          values.forEach(val => sl.insert(val))
          return sl.getSize() >= this.config.threshold
        }
      }
      return slConfigObj.processWithConfig([1, 2, 3]) === true
    })()
  )

  console.log(
    "10. 跳表一致性测试:",
    (() => {
      const consistencySl = new SkipList()
      consistencySl.insert(5)
      consistencySl.insert(3)
      consistencySl.insert(7)
      const size1 = consistencySl.getSize()
      consistencySl.delete(3)
      const size2 = consistencySl.getSize()
      return size1 === 3 && size2 === 2
    })()
  )

  console.log(
    "11. 跳表边界测试:",
    (() => {
      const boundarySl = new SkipList()
      boundarySl.insert(1)
      return boundarySl.getMin() === 1 &&
             boundarySl.getMax() === 1 &&
             boundarySl.getSize() === 1
    })()
  )

  console.log(
    "12. 跳表性能测试:",
    (() => {
      const perfSl = new SkipList()
      const values = Array.from({ length: 100 }, (_, i) => i + 1)
      const start = performance.now()
      values.forEach(val => perfSl.insert(val))
      values.forEach(val => perfSl.search(val))
      const end = performance.now()
      return perfSl.getSize() === 100 && (end - start) < 1000
    })()
  )
}

testSkipListBasic()
